const express= require('express');
const common = require('../../../libs/common');
const reg = require('../../../libs/reg');

let router = express.Router();
module.exports = router;

router.post('/',(req,res)=>{
    let {email,password,rePwd,graphCode,emailcode} = req.body;
    // 做效验
   
    if(!reg.mailReg.test(email)){
        let regData={
            status:2,
            errMsg:"Please enter a valid email address"
        }
        res.send(regData)
    }else if(!reg.pwdReg.test(password)){
        let regData={
            status:2,
            errMsg:"Enter a combination of letters and numbers between 5 and 21"
        }
        res.send(regData)
    }else if(!reg.pwdReg.test(rePwd)){
        let regData={
            status:2,
            errMsg:"Enter a combination of letters and numbers between 5 and 21"
        }
        res.send(regData)
    }else if(password!=rePwd){
        let regData={
            status:2,
            errMsg:"The password and its confirm are not the same"
        }
        res.send(regData)
    }else if(!reg.graphCodeReg.test(graphCode)){
        let regData={
            status:2,
            errMsg:"Enter valid graphic verification code"
        }
        res.send(regData)
    }else if(!reg.emailcodeReg.test(emailcode)){
        let regData={
            status:2,
            errMsg:"Enter six digits of mailbox verification code"
        }
        res.send(regData)
    }else if(graphCode!=req.session['car_captcha']){
        let regData={
            status:3,
            errMsg:"Graphic captcha error"
        }
        res.send(regData)
    }else{
        // 判断邮箱验证码是否正确
        req.db.query(`SELECT code,last_send_time,b_use FROM send_mail WHERE mail='${email}'`, (err, data) => {
            if (err) {
                res.sendStatus(500);
            } else if(data ==0){
                let regData={
                    status:2, 
                    errMsg:"Please send verification code"
                }
                res.send(regData)
            } else {
                if(data[0].code != emailcode){
                    let regData={
                        status:2,
                        errMsg:"Email verification code error"
                    }
                    res.send(regData)
                }else if((Number(data[0].last_send_time)+120)<=Number(common.timestamp())){
                    // 判断邮箱验证码是否过期
                    let regData={
                        status:2,
                        errMsg:"The email verification code has expired. Please resend it"
                    }
                    res.send(regData)
                }else if(data[0].b_use[0] == 1){
                    let regData={
                        status:2,
                        errMsg:"The email verification code has expired. Please resend it"
                    }
                    res.send(regData)
                }else{
                    req.db.query(`UPDATE send_mail SET b_use=1 WHERE mail='${email}'`,(err,data)=>{
                        if(err){
                            res.sendStatus(500);
                        }else{
                            req.db.query(`SELECT ID FROM user WHERE username='${email}'`, (err, data) => {
                                if(err){
                                    res.sendStatus(500);
                                }else if(data == 0){
                                    // 生成UUID
                                    function createID(fn) {
                                        let id = common.uuid();
                                        req.db.query(`SELECT ID FROM user WHERE ID='${id}'`, (err, data) => {
                                            if (err) {
                                                res.sendStatus(500);
                                            } else if (data.length > 0) {
                                                createID();
                                            } else {
                                                fn(id);
                                            }
                                        })
                                    }
                                    createID(function(id){
                                        req.db.query(`INSERT INTO user (id,username,password) VALUES ('${id}','${email}','${common.md5(password)}')`,(err,data)=>{
                                            if(err){
                                                res.sendStatus(500);
                                            }else{
                                                let regData={
                                                    status:1
                                                }
                                                res.send(regData)
                                            }
                                        })
                                    })
                                }else{
                                    let regData={
                                        status:2,
                                        errMsg:"Email has been registered"
                                    }
                                    res.send(regData)
                                }
                            })
                        }
                    })
                }
            }
        })
        
    }
})
